package in.blogspot.khurram2java;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbAdapter {
	private static final String DATABASE_NAME = "DemoDatabase.db";
	private static final String DATABASE_TABLE = "Users";
	private static final int DATABASE_VERSION = 1;
	
	// The index (key) 
	public static final String KEY_ID = "_id";
	
	// The name and column index of each column in your database.
	public static final String KEY_ISBN = "isbn";
	public static final String KEY_NAME = "name";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_AUTHOR = "author";
	public static final String KEY_STYLE = "style";
	
	// SQL Statement to create a new database.
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_ISBN  + " text not null, " + KEY_NAME
			+ " text not null, " + KEY_EMAIL + " text not null ,  " + KEY_AUTHOR + " text not null, " + KEY_STYLE + " text not null  " + ");";
	
	// variable to hold the database instance
	private SQLiteDatabase db;
	
	// Context of the application using the database.
	private final Context context;
	// Database open/upgrade helper
	private myDbHelper dbHelper;

	public DbAdapter(Context _context) {

		context = _context;
		dbHelper = new myDbHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
	}

	public DbAdapter open() throws SQLException {
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	public void addEntry(Info _info) {
		//Create a new ContentValues to represent my row
		// and insert it into the database.
		open();
		ContentValues values = new ContentValues();
		values.put(KEY_ISBN, _info.getIsbn()); // Contact Isbn
		values.put(KEY_NAME, _info.getName()); // Contact Name
		values.put(KEY_EMAIL, _info.getEmail());// Contact email
		values.put(KEY_AUTHOR, _info.getAuthor()); // Contact Author
		values.put(KEY_STYLE, _info.getStyle()); // Contact Style

		// Inserting Row
		db.insert(DATABASE_TABLE, null, values);
		close(); // Closing database connection
		//return index;
	}

	public void removeEntry(Info _info) {
		db.delete(DATABASE_TABLE, KEY_ID + " = ?",
				new String[] { String.valueOf(_info.getID()) });
		close();
	}

	public List<Info> getAllInfo() {
		List<Info> infoList = new ArrayList<Info>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;
        open();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				Info info = new Info();
				info.setID(Integer.parseInt(cursor.getString(0)));
				info.setIsbn(cursor.getString(1));
				info.setName(cursor.getString(2));
				info.setEmail(cursor.getString(3));
				info.setAuthor(cursor.getString(4));
				info.setStyle(cursor.getString(5));
				// Adding contact to list
				infoList.add(info);
			} while (cursor.moveToNext());
		}
        cursor.close();
        close();
		// return contact list
		return infoList;
	}
	public String[] getItems(String KEY_ITEM) {
		String selectQuery = "SELECT " + "*" + " FROM " + DATABASE_TABLE;
        open();
		Cursor cursor = db.rawQuery(selectQuery, null);
		String[] items = new String[cursor.getCount()];
		int i=0;
		if(cursor.moveToFirst()){
			do {
				
			        String item = cursor.getString(cursor.getColumnIndex(KEY_ITEM));
				    items[i] = item;
				    i++;
				
			} while (cursor.moveToNext());
		}
		
		cursor.close();
		close();
	    return items;
	}


	public int updateEntry(Info _info) {

		ContentValues values = new ContentValues();
		values.put(KEY_ISBN, _info.getIsbn());
		values.put(KEY_NAME, _info.getName());
		values.put(KEY_EMAIL, _info.getEmail());
		values.put(KEY_AUTHOR, _info.getAuthor());
		values.put(KEY_STYLE, _info.getStyle());

		// updating row
		return db.update(DATABASE_TABLE, values, KEY_ID + " = ?",
				new String[] { String.valueOf(_info.getID()) });
	}

	private static class myDbHelper extends SQLiteOpenHelper {
		public myDbHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		// Called when no database exists in disk and the helper class needs
		// to create a new one.
		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(DATABASE_CREATE);
		}

		// Called when there is a database version mismatch meaning that the
		// version
		// of the database on disk needs to be upgraded to the current version.
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {
			// Log the version upgrade.
			Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion
					+ " to " + _newVersion
					+ ", which will destroy all old data");
			
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			// Create a new one.
			onCreate(_db);
		}
	}
}

